package
{
	import flash.events.MouseEvent;
	import flash.filters.GlowFilter;
	import flash.geom.Point;
	import flash.media.Sound;
	
	public class GMStorePlacement implements IGameMode
	{
		public static const MODE_BUY:int = 0;
		public static const MODE_MOVE:int = 1;
		
		private var store:Store;
		private var m_mall:Mall;
		private var m_mode:int;
		
		public var storeReference:Store;
		
		public function GMStorePlacement(storeName:String, mode:int)
		{
			m_mode = mode;
			m_mall = Game.instance.mall;
			
			store = new Store();
			store.init(storeName);
			store.display.buttonMode = true;
			store.display.alpha = .7;
			m_mall.showAllowableArea();
			m_mall.previewStore(store);
		}
		
		
		public function onMouseMove(me:MouseEvent):void{
			var stagePoint:Point = new Point(me.stageX, me.stageY);
			var gamePoint:Point = m_mall.stage2me(stagePoint);
			var mouseLocation:Point = new Point(Math.floor(gamePoint.x), Math.floor(gamePoint.y));
			store.location = mouseLocation;
			
			
			if(!Game.instance.findSelectedObject(stagePoint) && m_mall.allowableRectangle.containsRect(store.getRectangle()) && Game.instance.canAffordStore(store)){
				store.display.filters = [new GlowFilter(0x00ff00, 1, 5,5,4)];
			} else {
				store.display.filters = [new GlowFilter(0xff0000, 1, 5,5,4)];
			}
			

		}
		
		public function onMouseUp(me:MouseEvent):void{
			var gamePoint:Point = m_mall.stage2me(new Point(me.stageX, me.stageY));
			var mouseLocation:Point = new Point(Math.floor(gamePoint.x), Math.floor(gamePoint.y));
			
			switch(m_mode){
				case MODE_MOVE:
					Game.instance.moveStore(storeReference, mouseLocation);
					break;
				case MODE_BUY:
					if(Game.instance.canAffordStore(store) && m_mall.allowableRectangle.containsRect(store.getRectangle())){
						Game.instance.buyStore(store.name, mouseLocation);
						SoundManager.play('assets/buyStore.mp3');
					} else {
						SoundManager.play('assets/error.mp3');
					}
					break;
			}
			

		}
		
		public function onMouseDown(me:MouseEvent):void{
			
		}
		
		public function cleanup():void{
			store.display.visible = false;
			store.display.parent.removeChild(store.display);
		}
	}
}